import numpy as np
from scipy.stats import pearsonr
from PIL import Image

def convert_32bit_to_24bit(image):
    # 检查图像的模式
    if image.mode == 'RGBA':
        # 去掉Alpha通道
        image = image.convert('RGB')
    return image

def calculate_pearson_correlation(image_path1, image_path2):
    # 加载图像
    image1 = Image.open(image_path1)
    image2 = Image.open(image_path2)

    # 检查并转换图像位深度
    image1 = convert_32bit_to_24bit(image1)
    image2 = convert_32bit_to_24bit(image2)

    # 将图像转换为数组
    array1 = np.array(image1)
    array2 = np.array(image2)

    # 展平数组
    flat_array1 = array1.flatten()
    flat_array2 = array2.flatten()

    # 计算皮尔逊相关系数
    correlation, _ = pearsonr(flat_array1, flat_array2)

    return correlation


# a1 = calculate_pearson_correlation("D:/bitahub_data/mb_30/mb_test/out/img_512nm/00125/S125M1-124_tr1-tc1.png",
#                                    "D:/bitahub_data/mb_30/mb_test/out/img_512nm/00126/S126M1-127_tr1-tc1.png")
# print(a1)
#
# a2 = calculate_pearson_correlation("D:/bitahub_data/mb_30/mb_test/out/img_512nm/00126/S126M1-127_tr1-tc1.png",
#                                    "D:/bitahub_data/mb_30/mb_test/out/img_512nm/00127/S127M1-124_tr1-tc1.png")
# print(a2)
#
# a3 = calculate_pearson_correlation("D:/bitahub_data/mb_30/mb_test/out/img_512nm/00127/S127M1-124_tr1-tc1.png",
#                                    "D:/bitahub_data/mb_30/mb_test/out/img_512nm/00128/S128M1-126_tr1-tc1.png")
# print(a3)
#
# a4 = calculate_pearson_correlation("D:/bitahub_data/mb_30/mb_test/out/img_512nm/00128/S128M1-126_tr1-tc1.png",
#                                    "D:/bitahub_data/mb_30/mb_test/out/img_512nm/00129/S129M1-127_tr1-tc1.png")
# print(a4)

# a1 = calculate_pearson_correlation("D:/bitahub_data/mb_30/mb_test/out/img_128nm/00125/S125M1-124_tr3-tc2.png",
#                                    "D:/bitahub_data/mb_30/mb_test/out/img_128nm/00126/S126M1-127_tr3-tc2.png")
# print(a1)
#
# a2 = calculate_pearson_correlation("D:/bitahub_data/mb_30/mb_test/out/img_128nm/00126/S126M1-127_tr3-tc2.png",
#                                    "D:/bitahub_data/mb_30/mb_test/out/img_128nm/00127/S127M1-124_tr3-tc2.png")
# print(a2)
#
# a3 = calculate_pearson_correlation("D:/bitahub_data/mb_30/mb_test/out/img_128nm/00127/S127M1-124_tr3-tc2.png",
#                                    "D:/bitahub_data/mb_30/mb_test/out/img_128nm/00128/S128M1-126_tr3-tc2.png")
# print(a3)
#
# a4 = calculate_pearson_correlation("D:/bitahub_data/mb_30/mb_test/out/img_128nm/00128/S128M1-126_tr3-tc2.png",
#                                    "D:/bitahub_data/mb_30/mb_test/out/img_128nm/00129/S129M1-127_tr3-tc2.png")
# print(a4)

a1 = calculate_pearson_correlation("E:\\wafer52\\11866_2048nm\\S11866M3-163_tr1-tc1（2）.png",
                                   "E:\\wafer52\\11867_2048nm\\S11867M1-80_tr1-tc1(2).png")
print(a1)

a2 = calculate_pearson_correlation("E:\\wafer52\\11866_2048nm\\S11866M3-163_tr1-tc1（2）.png",
                                   "E:\\wafer52\\11866_2048nm\\000001.png")
print(a2)

# a3 = calculate_pearson_correlation("D:/bitahub_data/mb_30/mb_test/out/img_32nm/00127/S127M1-124_tr12-tc12.png",
#                                    "D:/bitahub_data/mb_30/mb_test/out/img_32nm/00128/S128M1-126_tr12-tc12.png")
# print(a3)
#
# a4 = calculate_pearson_correlation("D:/bitahub_data/mb_30/mb_test/out/img_32nm/00128/S128M1-126_tr12-tc12.png",
#                                    "D:/bitahub_data/mb_30/mb_test/out/img_32nm/00129/S129M1-127_tr12-tc12.png")
# print(a4)
